package com.siwei.mes.service.experiment.impl;

import com.siwei.mes.common.util.DateHelper;
import com.siwei.mes.entity.experiment.ExperimentCheckWaybillWeek;
import com.siwei.mes.mapper.ExperimentCheckWaybillWeekMapper;
import com.siwei.mes.service.experiment.ExperimentCheckWaybillWeekService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

import static com.siwei.mes.common.exception.handler.MessageHandler.assertTrue;

/**
 * 快检/批检对应按时间生成的配置信息(ExperimentCheckWaybillWeek)表服务实现类
 *
 * @author linzi
 * @since 2025-03-09 22:58:41
 */
@Service
public class ExperimentCheckWaybillWeekServiceImpl implements ExperimentCheckWaybillWeekService {
    @Resource
    private ExperimentCheckWaybillWeekMapper experimentCheckWaybillWeekMapper;

    @Override
    public Long create(ExperimentCheckWaybillWeek entity) {
        experimentCheckWaybillWeekMapper.insertSelective(entity);
        return entity.getId();
    }

    @Override
    public void update(ExperimentCheckWaybillWeek entity) {
        ExperimentCheckWaybillWeek ExperimentCheckWaybillWeek = experimentCheckWaybillWeekMapper.selectByPrimaryKey(entity.getId());
        assertTrue(ExperimentCheckWaybillWeek == null, "快检/批检对应按时间生成的配置信息信息不存在");
        experimentCheckWaybillWeekMapper.updateByPrimaryKeySelective(entity);
    }

    @Override
    public void delete(Long id) {
        ExperimentCheckWaybillWeek ExperimentCheckWaybillWeek = experimentCheckWaybillWeekMapper.selectByPrimaryKey(id);
        assertTrue(ExperimentCheckWaybillWeek == null, "快检/批检对应按时间生成的配置信息信息不存在");
        experimentCheckWaybillWeekMapper.deleteByPrimaryKey(id);
    }

    @Override
    public ExperimentCheckWaybillWeek getConfig(ExperimentCheckWaybillWeek entity) {
        ExperimentCheckWaybillWeek checkWaybillWeek = experimentCheckWaybillWeekMapper.selectNewInfo(entity);
        if (checkWaybillWeek == null) {
            checkWaybillWeek = new ExperimentCheckWaybillWeek();
            checkWaybillWeek.setCheckType(entity.getCheckType());
            checkWaybillWeek.setExperimentType(entity.getExperimentType());
            checkWaybillWeek.setProjectSpec(entity.getProjectSpec());
            checkWaybillWeek.setProjectName(entity.getProjectName());
            checkWaybillWeek.setSupplyCompanyCompany(entity.getSupplyCompanyCompany());
            checkWaybillWeek.setFactory(entity.getFactory());
            String weekNumber = DateHelper.getWeekNumber(new Date());
            checkWaybillWeek.setWeekNumber(Integer.valueOf(weekNumber));
            this.create(checkWaybillWeek);
        }
        return checkWaybillWeek;
    }
}
